package com.ruoyi.web.controller.qsng;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.qsng.domain.KCoursePurchase;
import com.ruoyi.qsng.service.IKCoursePurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程购买Controller
 *
 * @author kanglou
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/qsng/coursePurchase")
@Api(tags = "pc--课程购买Controller")
public class KCoursePurchaseController extends BaseController {
    @Autowired
    private IKCoursePurchaseService kCoursePurchaseService;

    /**
     * 查询课程购买列表
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询课程购买列表")
    public TableDataInfo list(KCoursePurchase kCoursePurchase) {
        startPage();
        List<KCoursePurchase> list = kCoursePurchaseService.selectKCoursePurchaseDataList(kCoursePurchase);
        return getDataTable(list);
    }

    /**
     * 导出课程购买列表
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:export')")
    @Log(title = "课程购买", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出课程购买列表")
    public void export(HttpServletResponse response, KCoursePurchase kCoursePurchase) {
        List<KCoursePurchase> list = kCoursePurchaseService.selectKCoursePurchaseDataList(kCoursePurchase);
        for (KCoursePurchase ls :list) {
            //0=未购买,1=已购买，2已退款
            if(ls.getState() == 0){
                ls.setStateName("未购买");
            }else if(ls.getState() == 1){
                ls.setStateName("已购买");
            }else if(ls.getState() == 2){
                ls.setStateName("已退款");
            }
        }
//        for (KCoursePurchase kCoursePurchase1:list) {
//            kCoursePurchase1.setRemainingQuota2(Math.toIntExact(kCoursePurchase1.getTotalQuota() - kCoursePurchase1.getSold()));
//        }
        ExcelUtil<KCoursePurchase> util = new ExcelUtil<KCoursePurchase>(KCoursePurchase.class);
        util.exportExcel(response, list, "课程购买数据");
    }

    /**
     * 获取课程购买详细信息
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取课程购买详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(kCoursePurchaseService.selectKCoursePurchaseById(id));
    }

    /**
     * 新增课程购买
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:add')")
    @Log(title = "课程购买", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增课程购买")
    public AjaxResult add(@RequestBody KCoursePurchase kCoursePurchase) {
        return toAjax(kCoursePurchaseService.insertKCoursePurchase(kCoursePurchase));
    }

    /**
     * 修改课程购买
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:edit')")
    @Log(title = "课程购买", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改课程购买")
    public AjaxResult edit(@RequestBody KCoursePurchase kCoursePurchase) {
        return toAjax(kCoursePurchaseService.updateKCoursePurchase(kCoursePurchase));
    }

    /**
     * 删除课程购买
     */
    @PreAuthorize("@ss.hasPermi('qsng:coursePurchase:remove')")
    @Log(title = "课程购买", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除课程购买")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(kCoursePurchaseService.deleteKCoursePurchaseByIds(ids));
    }
}
